package com.example.recommendationservice.service.impl;

import com.example.recommendationservice.entity.ProductsTags;
import com.example.recommendationservice.entity.UsersTags;
import com.example.recommendationservice.entity.UserBrowseRecord;
import com.example.recommendationservice.entity.UserPurchaseRecord;
import com.example.recommendationservice.repository.ProductsTagsRepository;
import com.example.recommendationservice.repository.UsersTagsRepository;
import com.example.recommendationservice.repository.UserBrowseRecordRepository;
import com.example.recommendationservice.repository.UserPurchaseRecordRepository;
import com.example.recommendationservice.service.LLMService;
import com.example.recommendationservice.service.RecommendationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.cache.annotation.Cacheable;

import java.util.List;

@Service
public class RecommendationServiceImpl implements RecommendationService {

    @Autowired
    private UsersTagsRepository usersTagsRepository;

    @Autowired
    private ProductsTagsRepository productsTagsRepository;

    @Autowired
    private UserBrowseRecordRepository userBrowseRecordRepository;

    @Autowired
    private UserPurchaseRecordRepository userPurchaseRecordRepository;

    @Autowired
    private LLMService llmService;

    @Override
    public List<String> getRecommendations(String userId) {
        // 根据用户 ID 获取用户信息
        UsersTags usersTags = usersTagsRepository.findById(userId).orElse(null);
        if (usersTags == null) {
            return List.of();
        }

        // 获取所有用户信息
        List<UsersTags> allUsersTags = usersTagsRepository.findAll();

        // 获取用户的浏览记录
        List<UserBrowseRecord> userBrowseRecords = userBrowseRecordRepository.findByUserId(userId);

        // 获取用户的购买记录
        List<UserPurchaseRecord> userPurchaseRecords = userPurchaseRecordRepository.findByUserId(userId);

        // 获取所有产品信息
        List<ProductsTags> allProductsTags = productsTagsRepository.findAll();

        // 获取所有用户的浏览记录
        List<UserBrowseRecord> allUserBrowseRecords = userBrowseRecordRepository.findAll();

        // 获取所有用户的购买记录
        List<UserPurchaseRecord> allUserPurchaseRecords = userPurchaseRecordRepository.findAll();

        // 调用 LLMService 获取推荐商品
        return llmService.getRecommendations(usersTags, allUsersTags, allProductsTags, userBrowseRecords, userPurchaseRecords, allUserBrowseRecords, allUserPurchaseRecords);
    }
}